//*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>

// 定义回调函数，处理客户端发送的数据
void on_event(struct bufferevent *bev, short events, void *ctx)
{
    if (events & BEV_EVENT_EOF)
    {
        printf("Connection closed.\n");
    }
    else if (events & BEV_EVENT_ERROR)
    {
        printf("Some other error.\n");
    }
    else if (events & BEV_EVENT_CONNECTED)
    {
        printf("Connected!\n");
    }

    // 向客户端发送消息
    const char *msg = "Hello, World!\n";
    bufferevent_write(bev, msg, strlen(msg));
}

// 定义回调函数，处理客户端连接
void on_accept(struct evconnlistener *listener, evutil_socket_t fd,
               struct sockaddr *addr, int socklen, void *ctx)
{
    struct event_base *base = (struct event_base *)ctx;
    struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);

    // 设置回调函数，处理客户端发送的数据
    bufferevent_setcb(bev, NULL, NULL, on_event, NULL);
    bufferevent_enable(bev, EV_READ | EV_WRITE);
}

int main(int argc, char **argv)
{
    struct event_base *base = event_base_new();
    struct sockaddr_in sa = {0};
    if (!base)
    {
        fprintf(stderr, "Could not initialize libevent!\n");
        return 1;
    }

    sa.sin_family = AF_INET;
    sa.sin_port = htons(9995);
    struct evconnlistener *listener = evconnlistener_new_bind(base, on_accept, (void *)base,
                                                              LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1,
                                                              (struct sockaddr *)&sa,
                                                              sizeof(sa));
    if (!listener)
    {
        fprintf(stderr, "Could not create a listener!\n");
        return 1;
    }

    event_base_dispatch(base);

    evconnlistener_free(listener);
    event_base_free(base);
    return 0;
}

//*/

/*
  This example program provides a trivial server program that listens for TCP
  connections on port 9995.  When they arrive, it writes a short message to
  each client connection, and closes each connection once it is flushed.

  Where possible, it exits cleanly in response to a SIGINT (ctrl-c).
*/

// #include <string.h>
// #include <errno.h>
// #include <stdio.h>
// #include <signal.h>
// #ifndef _WIN32
// #include <netinet/in.h>
// #ifdef _XOPEN_SOURCE_EXTENDED
// #include <arpa/inet.h>
// #endif
// #include <sys/socket.h>
// #endif
//
// #include <event2/bufferevent.h>
// #include <event2/buffer.h>
// #include <event2/listener.h>
// #include <event2/util.h>
// #include <event2/event.h>
//
// static const char MESSAGE[] = "Hello, World!\n";
//
// static const unsigned short PORT = 9995;
//
// static void listener_cb(struct evconnlistener *, evutil_socket_t,
//                         struct sockaddr *, int socklen, void *);
// static void conn_writecb(struct bufferevent *, void *);
// static void conn_eventcb(struct bufferevent *, short, void *);
// static void signal_cb(evutil_socket_t, short, void *);
//
// int main(int argc, char **argv)
// {
//     struct event_base *base;
//     struct evconnlistener *listener;
//     struct event *signal_event;
//
//     struct sockaddr_in sin = {0};
// #ifdef _WIN32
//     WSADATA wsa_data;
//     WSAStartup(0x0201, &wsa_data);
// #endif
//
//     base = event_base_new();
//     if (!base)
//     {
//         fprintf(stderr, "Could not initialize libevent!\n");
//         return 1;
//     }
//
//     sin.sin_family = AF_INET;
//     sin.sin_port = htons(PORT);
//
//     listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
//                                        LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1,
//                                        (struct sockaddr *)&sin,
//                                        sizeof(sin));
//
//     if (!listener)
//     {
//         fprintf(stderr, "Could not create a listener!\n");
//         return 1;
//     }
//
//     signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
//
//     if (!signal_event || event_add(signal_event, NULL) < 0)
//     {
//         fprintf(stderr, "Could not create/add a signal event!\n");
//         return 1;
//     }
//
//     event_base_dispatch(base);
//
//     evconnlistener_free(listener);
//     event_free(signal_event);
//     event_base_free(base);
//
//     printf("done\n");
//     return 0;
// }
//
// static void
// listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
//             struct sockaddr *sa, int socklen, void *user_data)
// {
//     struct event_base *base = (struct event_base *)user_data;
//     struct bufferevent *bev;
//
//     bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
//     if (!bev)
//     {
//         fprintf(stderr, "Error constructing bufferevent!");
//         event_base_loopbreak(base);
//         return;
//     }
//     bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
//     bufferevent_enable(bev, EV_WRITE);
//     bufferevent_disable(bev, EV_READ);
//
//     bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
// }
//
// static void
// conn_writecb(struct bufferevent *bev, void *user_data)
// {
//     struct evbuffer *output = bufferevent_get_output(bev);
//     if (evbuffer_get_length(output) == 0)
//     {
//         printf("flushed answer\n");
//         bufferevent_free(bev);
//     }
// }
//
// static void
// conn_eventcb(struct bufferevent *bev, short events, void *user_data)
// {
//     if (events & BEV_EVENT_EOF)
//     {
//         printf("Connection closed.\n");
//     }
//     else if (events & BEV_EVENT_ERROR)
//     {
//         printf("Got an error on the connection: %s\n",
//                strerror(errno)); /*XXX win32*/
//     }
//     /* None of the other events can happen here, since we haven't enabled
//      * timeouts */
//     bufferevent_free(bev);
// }
//
// static void
// signal_cb(evutil_socket_t sig, short events, void *user_data)
// {
//     struct event_base *base = (struct event_base *)user_data;
//     struct timeval delay = {2, 0};
//
//     printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
//
//     event_base_loopexit(base, &delay);
// }